package com.baofu.paycore.service.factory;

import com.baofu.paycore.common.utils.PayNoGenerateUtil;
import com.baofu.paycore.manager.model.PayBaseBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.model.PayBaseDTO;
import com.baofu.paycore.service.processor.PayBaseProcessor;
import com.google.common.collect.Lists;
import com.system.commons.sequence.redis.SequenceFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 构建订单对象
 *
 * <p/>
 *      1、组装订单
 * </p>
 * User: LZQ Date: 2016/04/20  Version: 1.0
 */
@Slf4j
public abstract class PayBaseFactory {

    /**
     * 支付序号
     */
    @Autowired
    private SequenceFacade sequenceFacade;

    /**
     * 支付主单对象处理器
     */
    protected List<PayBaseProcessor> processor = Lists.newArrayList();

    /**
     * 领域模型组装
     *
     * @param payBaseBO      支付请求参数BO
     * @param payBaseDTO     支付请求参数DTO
     */
    public void buildBO(PayBaseBO payBaseBO, PayBaseDTO payBaseDTO) {

        //支付单号构建
        payBaseBO.setPayNo(PayNoGenerateUtil.payNoGenerate(sequenceFacade, payBaseDTO.getServiceCode()));
        //支付单对象组装 多线程处理注意处理顺序以免赋值影响
        for(PayBaseProcessor payBaseProcessor: processor){
            payBaseProcessor.process(payBaseBO,payBaseDTO);
        }
        payBaseBO.consistencyCheck();
    }

    public Boolean getRealRtnFlag(String status){

        PayStatusEnums enums = PayStatusEnums.explain(status);
        switch (enums){
            case FAIL:
                return Boolean.TRUE;
            case SUCCESS:
                return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
